#!/usr/bin/env node

const http = require('http');
const fs = require('fs');
const text = require('./data.js');

http.createServer((req,res)=>{
  if(req.url=='/'){
    show(res);
  }
  if(req.url=='/getlist'){
    let dataList = text.filter(item=>{
      if(item.movieRange !== '暂无评分'){
        return item;
      }
    })
    for(let i = 0; i < dataList.length - 1;i++){
      for(let j = 0; j < dataList.length - i - 1; j++){
        if(dataList[j+1].movieRange > dataList[j].movieRange){
          let data = dataList[j+1];
          dataList[j+1] = dataList[j];
          dataList[j] = data;
        }          
      }
    }
    res.end(JSON.stringify(dataList));
  }
}).listen(3000);

function show(res){
  let html = fs.readFileSync('./index.html').toString('utf-8');
  res.setHeader('Content-Type','text/html');
  res.end(html);
} 
